package com.czl.module_base.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.czl.module_base.R;
import com.czl.module_base.widget.CustomPopWindow;

/**
 * @author by xubin,
 * @date on 2022/2/11.
 * PS: Not easy to write code, please indicate.
 */
public class PopWindowUtils {
    private static PopWindowUtils instance;
    private CustomPopWindow mPopWindow;

    /**
     * 私有化构造方法，变成单例模式
     */
    private PopWindowUtils() {

    }

    /**
     * 对外提供一个该类的实例，考虑多线程问题，进行同步操作
     *
     * @return
     */
    public static PopWindowUtils getInstance() {
        if (instance == null) {
            synchronized (PopWindowUtils.class) {
                if (instance == null) {
                    instance = new PopWindowUtils();
                }
            }
        }
        return instance;
    }

    public void createPopWindowBase(Context context, View contentView, int width, int height,
                                    float alpha, int mode, int style, boolean focusable,
                                    boolean outsideTouchable) {
        //构建默认的PopWindow
        CustomPopWindow.PopupWindowBuilder builder = new CustomPopWindow.PopupWindowBuilder(context);
        if (mPopWindow == null) {
            mPopWindow = builder.setView(contentView)
                    .size(width, height)
                    .setFocusable(focusable)
                    .setTouchable(true)
                    .setInputMethodMode(mode)
                    .setOutsideTouchable(outsideTouchable)
                    .enableBackgroundDark(true)
                    .setAnimationStyle(style)
                    .setBgDarkAlpha(alpha)
                    .setOnDissmissListener(() -> {
                        if (mPopWindow != null) {
                            mPopWindow = null;
                        }
                    })
                    .create();
        }
    }

    public void createPopWindow(Context context, View contentView, int width, int height, String text) {
        View layoutView = View.inflate(context, R.layout.layout_dialog_base, null);
        TextView baseTv = layoutView.findViewById(R.id.base_pop_tv);
        baseTv.setText(text);
        //构建默认的PopWindow
        CustomPopWindow.PopupWindowBuilder builder = new CustomPopWindow.PopupWindowBuilder(context);
        FrameLayout content = layoutView.findViewById(R.id.base_pop_content);
        content.addView(contentView);
        this.createPopWindow(context, layoutView, width, height);
        ImageView errorIv = layoutView.findViewById(R.id.base_pop_error_iv);
        errorIv.setOnClickListener(v -> dismissPopupWindow());
    }

    public void createPopWindow(Context context, View contentView, int width, int height, int style, String text) {
        View layoutView = View.inflate(context, R.layout.layout_dialog_base, null);
        TextView baseTv = layoutView.findViewById(R.id.base_pop_tv);
        baseTv.setText(text);
        //构建默认的PopWindow
        CustomPopWindow.PopupWindowBuilder builder = new CustomPopWindow.PopupWindowBuilder(context);
        FrameLayout content = layoutView.findViewById(R.id.base_pop_content);
        content.addView(contentView);
        this.createPopWindowBase(context, layoutView, width, height, 0.6f, -1, style, true, true);
        ImageView errorIv = layoutView.findViewById(R.id.base_pop_error_iv);
        errorIv.setOnClickListener(v -> dismissPopupWindow());
    }

    public void createDefaultPopWindow(Context context, View contentView, String text) {
        int width = (int) (ScreenUtils.getScreenWidth() * 0.8f);
        createPopWindow(context, contentView, width, text);
    }

    public void createPopWindow(Context context, View contentView, int width, String text) {
        createPopWindow(context, contentView, width, ViewGroup.LayoutParams.WRAP_CONTENT, text);
    }

    public void createPopWindow(Context context, View contentView) {
        //构建默认的PopWindow
        createPopWindow(context, contentView, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0.6f);
    }

    public void createPopWindow(Context context, View contentView, int width) {
        //构建默认的PopWindow
        createPopWindow(context, contentView, width, ViewGroup.LayoutParams.WRAP_CONTENT, 0.6f);
    }

    public void createPopWindow(Context context, View contentView, int width, int height) {
        //构建默认的PopWindow
        createPopWindow(context, contentView, width, height, 0.6f);
    }

    public void createPopWindow(Context context, View contentView, int width, int height, float alpha) {
        //构建默认的PopWindow
        this.createPopWindowBase(context, contentView, width, height, 0.6f, -1, -1, true, true);
    }

    public void createDefaultPopWindow(Context context, View contentView, int width, int height) {
        //构建默认的PopWindow
        this.createPopWindow(context, contentView, width, height);
    }

    public void createPopWindow(Context context, View contentView, String text, boolean errorIvVis,
                                boolean focusable, boolean outsideTouchable) {
        this.createPopWindow(context, contentView, text, errorIvVis, focusable, outsideTouchable, 0.6f);
    }

    public void createPopWindow(Context context, View contentView, String text, boolean errorIvVis,
                                boolean focusable, boolean outsideTouchable, float darkValue) {
        View layoutView = View.inflate(context, R.layout.layout_dialog_base, null);
        TextView baseTv = layoutView.findViewById(R.id.base_pop_tv);
        baseTv.setText(text);
        //构建默认的PopWindow
        FrameLayout content = layoutView.findViewById(R.id.base_pop_content);
        content.removeAllViews();
        content.addView(contentView);
        this.createPopWindowBase(context, layoutView,
                (int) (ScreenUtils.getScreenWidth() * 0.8f),
                ViewGroup.LayoutParams.WRAP_CONTENT, darkValue, -1, -1,
                focusable, outsideTouchable);

        ImageView errorIv = layoutView.findViewById(R.id.base_pop_error_iv);
        errorIv.setVisibility(errorIvVis ? View.VISIBLE : View.GONE);
        errorIv.setOnClickListener(v -> dismissPopupWindow());
    }

    public void dismissPopupWindow() {
        if (mPopWindow != null && mPopWindow.isShowing()) {
            mPopWindow.dismiss();
            mPopWindow = null;
        }
    }

    public boolean isShowing() {
        return mPopWindow != null && mPopWindow.isShowing();
    }

    public void showBasePopupWindow(View view) {
        if (mPopWindow != null && !mPopWindow.isShowing()) {
            mPopWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }

    public void showAtLocation(View view, int gravity) {
        if (mPopWindow != null && !mPopWindow.isShowing()) {
            mPopWindow.showAtLocation(view, gravity, 0, 0);
        }
    }

    public void showAtLocation(View view, int gravity, int x, int y) {
        if (mPopWindow != null && !mPopWindow.isShowing()) {
            mPopWindow.showAtLocation(view, gravity, x, y);
        }
    }

    public void showAsDropDown(View view, int gravity, int x, int y) {
        if (mPopWindow != null && !mPopWindow.isShowing()) {
            mPopWindow.showAsDropDown(view, x, y, gravity);
        }
    }

    public void showAsDropDown(View view, int x, int y) {
        if (mPopWindow != null && !mPopWindow.isShowing()) {
            mPopWindow.showAsDropDown(view, x, y);
        }
    }

    public void showAsDropDown(View view) {
        if (mPopWindow != null && !mPopWindow.isShowing()) {
            mPopWindow.showAsDropDown(view);
        }
    }

    public int getMeasuredWidth() {
        if (mPopWindow != null) {
            mPopWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            return mPopWindow.getContentView().getMeasuredWidth();
        }
        return 0;
    }

    public int getMeasuredHeight() {
        if (mPopWindow != null) {
            mPopWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            return mPopWindow.getContentView().getMeasuredHeight();
        }
        return 0;
    }

    public CustomPopWindow getmPopWindow() {
        return mPopWindow;
    }
}
